<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :mainImg="goodDetail.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName />
          <!-- 商品sku信息 -->
          <GoodsSku :goods="goodDetail" @change="getSku" />
          <!-- 数量 -->
          <XtxNumbox v-model="buyNum" :max="goodDetail.inventory" />
          <!-- 按钮 -->
          <XtxButton
            @click="addCart"
            size="large"
            type="primary"
            style="margin-top: 30px"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
              v-imglazy="img"
              v-for="(img, index) in goodDetail.details.pictures"
              :key="index"
            />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import Message from '@/components/Message/index.js'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsImage from './components/goods-image.vue'
import { findGoods } from '@/api/goods'
import { ref, provide, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName
  },

  setup () {
    const goodDetail = ref({})
    provide('goodDetail', goodDetail)
    const route = useRoute()
    const getGoodDetail = async () => {
      const res = await findGoods(route.params.id)
      goodDetail.value = res
    }

    getGoodDetail()
    const currSel = ref(null)
    const buyNum = ref(1)
    // 获取商品sku信息
    const getSku = (currSku) => {
      // console.log(currSku)
      if (currSku.skuId) {
        goodDetail.value.price = currSku.price
        goodDetail.value.oldPrice = currSku.oldPrice
        goodDetail.value.inventory = currSku.inventory

        // 存储有效SKU
        currSel.value = currSku
      }
    }

    const store = useStore()
    // 加入购物车
    async function addCart () {
      if (!currSel.value) return Message({ text: '请选择商品规格' })
      if (currSel.value.inventory === 0) return Message({ text: '商品已经卖完啦' })
      // 加入购物车的商品字段
      const addGoods = {
        id: goodDetail.value.id,
        name: goodDetail.value.name,
        picture: goodDetail.value.mainPictures[0],
        skuId: currSel.value.skuId,
        price: currSel.value.oldPrice,
        nowPrice: currSel.value.price,
        attrsText: currSel.value.specsText,
        stock: currSel.value.inventory,
        selected: true,
        isEffective: true,
        count: buyNum.value
      }

      try {
        const res = await store.dispatch('cart/addCartAction', addGoods)
        Message({ type: 'success', text: res })
      } catch (error) {

      }
    }

    return { goodDetail, getSku, buyNum, currSel, addCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>